<template>
	<view>
		<u-sticky offset-top="0">
			<uni-card :is-full="true">
				<u-row customStyle="margin-bottom: 10px">
					<u-col span="2">
						<u--image :showLoading="true" src="/static/logo.png" width="60px" height="60px"></u--image>
					</u-col>
					<u-col span="6">
						<text>谈话的艺术是听和被听的艺术——赫兹里特</text>
					</u-col>
					<u-col span="2">
						<u-icon 
							name="/static/icon/shanchu.png" 
							size="36" 
							label="全部删除" 
							labelPos="bottom" 
							labelSize="16"
							@click="delListClick = true"
						></u-icon>
					</u-col>
					<u-col span="2">
						<u-icon 
							name="/static/icon/zhengque.png" 
							size="36" label="全部已读" 
							labelPos="bottom" 
							labelSize="16"
							@click="upNotice()"
						></u-icon>
					</u-col>
				</u-row>
			</uni-card>
		</u-sticky>
		<view class="body-wrap">
			<!-- 消息列表 -->
			<view class="msg-list-wrap" v-for="(item,index) in noticesList" :key="index">
				<u-swipe-action>
					<u-swipe-action-item :options="options" v-model="swipe"  @click="editNotices(item)">
						<view class="msg-card" @click="readMsgPopupShow = true;msgData(item)">
							<view class="left-info">
								<u-avatar src="/static/logo.png" size="48"></u-avatar>
								<view class="msg-content">
									<view class="title u-line-1">
										<u-parse :content="item.noticeTitle"></u-parse>
									</view>
									<view class="content u-line-1">
										<u-parse :content="item.releasingAbout"></u-parse>
									</view>
								</view>
							</view>
							<view class="right-info">
								<text class="date">{{item.createTime}}</text>
								<u-icon name="checkbox-mark" size="28" v-if="item.noticeState==1"></u-icon>
								<u-icon name="more-dot-fill" size="28" v-if="item.noticeState==0"></u-icon>
							</view>
						</view>
					</u-swipe-action-item>
				</u-swipe-action>
			</view>

			<!-- 阅读消息 -->
			<u-popup :show="readMsgPopupShow" round="20" mode="center" closeable closeOnClickOverlay
				@close="readMsgPopupShow = false">
				<view class="popup-slot-wrap">
					<view class="popup-title">
						阅读消息
					</view>
					<view class="popup-body">
						<view class="read-card">
							<view class="user-info-wrap">
								<view class="user-info">
									<u-avatar src="/static/logo.png" size="48"></u-avatar>
									<view class="left-info">
										<view class="title">
											<u-parse :content="msg.noticeTitle" style="font-size: 26rpx;"></u-parse>
										</view>
										<view class="content">
											系统消息
										</view>
									</view>
								</view>
								<view class="right-info">
									<text class="date">{{msg.createTime}}</text>
								</view>
							</view>
							<view class="msg-wrap" style="width: 93%;">
								<u-parse :content="msg.releasingAbout" style="font-size: 30rpx;"></u-parse>
							</view>
						</view>
					</view>
				</view>
			</u-popup>
		</view>
		<view >
			<u-modal
				:content="content"
				:show="delListClick"
				showCancelButton
				closeOnClickOverlay
				@confirm="confirm"
				@cancel="cancel"
				@close="close"
			></u-modal>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content:"未读内容无法删除，是否确定删除所有通知",
				delListClick:false,
				swipe:"",
				options: [
					{
						text: '删除',
						style: {
							backgroundColor: '#f56c6c'
						}
					},
				],
				readMsgPopupShow: false,
				noticesUrl:{
					getNoticesUrl:"/notices/notices/getNotices",
					editNoticesUrl:"/notices/notices/edit",
					delNoticesUrl:"/notices/notices/deleteBatch",
					getAnnouncementByNameId:"/announcement/announcement/getAnnouncementByNameId",
					addAnnouncement:"/announcement/announcement/add",
					delAnnouncement:"/announcement/announcement/delAnnouncement",
				},
				noticesList:[],
				urlId:"",
				msg:{},
				noticesType:"",
				announcement:{id:"",noticesId:"",noticeNameId:"",noticeState:""},
				intervalID:"",
			}
		},
		created() {
			this.getNotices()
		},
		methods: {
			editNotices(){
				
			},
			upNotice(){
				/* for (let i = 0; i < this.noticesList.length; i++) {
					this.msgData(this.noticesList[i])
				} */
			},
			confirm() {
				this.delListClick = false
				this.delNotice()
			},
			cancel() {
				this.delListClick = false
			},
			close() {
				this.delListClick = false
			},
			delNotice(){
				let ids=""
				for (var i = 0; i < this.noticesList.length; i++) {
					if(
						this.noticesList[i].noticeState!="0" &&
						this.noticesList[i].noticeNameId!="0" &&
						this.noticesList[i].noticeNameId!="1" &&
						this.noticesList[i].noticeNameId!="2" &&
						this.noticesList[i].noticeNameId!="666"
					){
						ids=ids+this.noticesList[i].id+","
					}
				}
				this.$axios.delete(this.noticesUrl.delNoticesUrl+"?ids="+ids).then((res)=>{})
				this.$axios.delete(this.noticesUrl.delAnnouncement+"?noticeNameId="+this.urlId).then((res)={})
			},
			getNotices(){
				setInterval(() => {
				   const stu=uni.getStorageSync('stu')
					const user=uni.getStorageSync('user')
					if(stu.stuId==null || stu.stuId==undefined){
						this.urlId=user.userId
						this.noticesType=2
					}else{
						this.urlId=stu.stuId
						this.noticesType=1
					}
					this.$axios.get(this.noticesUrl.getNoticesUrl+"?noticeNameId="+this.urlId+"&noticesType="+this.noticesType).then((res)=>{
						if(res.data.success){
							this.noticesList=res.data.result
						}
					})
				}, 1000);
			},
			msgData(item){
				this.msg=item
				if(this.msg.noticeNameId==666 || this.msg.noticeNameId==this.noticesType){
					this.$axios.get(this.noticesUrl.getAnnouncementByNameId+"?noticesId="+this.msg.noticesId+"&noticeNameId="+this.urlId).then((res)=>{
						if(res.data.result===0){
							this.announcement.noticesId=this.msg.noticesId
							this.announcement.noticeNameId=this.urlId
							this.announcement.noticeState=1
							this.$axios.post(this.noticesUrl.addAnnouncement,this.announcement).then((res)=>{})
						}
					})	
				}else{
					this.msg.noticeState=1
					this.$axios.post(this.noticesUrl.editNoticesUrl,this.msg).then((res)=>{})
				}
			}
		}
	}
</script>

<style lang="scss">
	.msg-list-wrap {
		background-color: #FFFFFF;
		padding: 24rpx;
		width: 700rpx;

		.msg-card {
			
			&:last-child {
				margin-bottom: 0rpx;
				border-bottom: none;
			}
			
			display: flex;
			align-items: flex-start;
			justify-content: space-between;
			border-bottom: 1rpx solid $u-border-color;
			margin-bottom: 20rpx;

			padding-bottom: 20rpx;

			.left-info {
				display: flex;
				align-items: center;

				.msg-content {
					margin-left: 20rpx;

					.title {
						font-size: $u-p;
						margin-bottom: 10rpx;
					}

					.content {
						max-width: 480rpx;
						color: $u-content-color;
						font-size: $u-p3;
					}
				}
			}

			.right-info {
				display: flex;
				flex-direction: column;
				align-items: flex-end;
				justify-content: space-between;
				height: 80rpx;
				color: $u-content-color;

				.date {
					font-size: $u-sub;
				}
			}
		}
	}

	.popup-slot-wrap {
		width: 650rpx;
		min-height: 400rpx;
		// padding: 24rpx;

		.read-card {
			padding: 24rpx;
			min-height: 200rpx;

			display: flex;
			flex-direction: column;
			align-items: flex-start;

			.user-info-wrap {
				width: 100%;
				display: flex;
				justify-content: space-between;

				.user-info {
					margin-bottom: 20rpx;
					display: flex;


					.left-info {
						margin-left: 20rpx;

						.title {
							font-size: $u-p;
							margin-bottom: 10rpx;
						}

						.content {
							max-width: 480rpx;
							color: $u-content-color;
							font-size: $u-p3;
						}
					}


				}

				.right-info {
					display: flex;
					flex-direction: column;
					align-items: flex-end;
					justify-content: space-between;
					height: 80rpx;
					color: $u-content-color;

					.date {
						font-size: $u-sub;
					}
				}
			}

			.msg-wrap {
				color: $u-content-color;
				padding: 20rpx;
				border-radius: 10rpx;
				background-color: $u-bg-color;
			}
		}
	}
	
	
	
	
</style>
